package com.ph.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.RandomStringUtils;

import com.ph.entity.MemberInfo;
import com.ph.shopping.common.util.rsa.MD5;

public class SessionUtil {

	private SessionUtil(){}
	
	/**
	 * 随机字符集
	 */
	private static final String RAND = "qwertyuiopasdfghjklzxcvbnm0123456789";
	/**
	 * 形成随机加密的字符集
	 */
	private static final String SESSION_KEY = MD5.getMD5Str(RandomStringUtils.random(RAND.length(), RAND));
	/**
	 * 
	* @Title: sessionAddAttribute  
	* @Description: session 赋值  
	* @param @param name
	* @param @param obj
	* @param @param request    参数  
	* @return void    返回类型  
	* @throws
	 */
	public static void setAttribute(MemberInfo obj,HttpServletRequest request){
		HttpSession session = request.getSession(true);
		session.setAttribute(SESSION_KEY, obj);
	}
	/**
	 * 
	* @Title: getAttribute  
	* @Description: session 取值  
	* @param @param name
	* @param @param request
	* @param @return    参数  
	* @return Object    返回类型  
	* @throws
	 */
	public static MemberInfo getAttribute(HttpServletRequest request){
		HttpSession session = request.getSession(true);
		Object obj = session.getAttribute(SESSION_KEY);
		if(obj instanceof MemberInfo){
			return (MemberInfo)obj;
		}
		return null;
	}
	/**
	 * 
	* @Title: removeAttribute  
	* @Description: 清楚缓存  
	* @param @param request    参数  
	* @return void    返回类型  
	* @throws
	 */
	public static void removeAttribute(HttpServletRequest request){
		HttpSession session = request.getSession();
		if(session != null){
			session.removeAttribute(SESSION_KEY);
		}
	}
	/**
	 * 
	* @Title: getKey  
	* @Description: 获取session key 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	public static String getKey(){
		
		return SESSION_KEY;
	}
	
}
